var SELL_PATH_PREFIX="/scbp/business/prdSellChannel";

function setSellChannelPath(path){
	SELL_PATH_PREFIX = path;
}

// 销售渠道列表
function prdSellChannelList(subPrdCode){
	$("#prdSellChannelList").jqGrid({
		caption: '销售渠道列表',
		url: '/scbp/business/prdSellChannel/prdSellChannelListPage.do',
		datatype: 'local',
		colNames:["销售编号","销售渠道","是否采用电子合同",'版本号','募集起始日','募集结束日'],
		colModel:[  { name: 'channelId', index: 'channelId',hidden:true, key: true, resizable: true, sortable: false },  
					{ name: 'seatName', index: 'seatName', resizable: true, sortable: false },
					{ name: 'isEContractNm', index: 'isEContract', resizable: true, sortable: false },
					{ name: 'versionNum', index: 'versionNum', resizable: true, sortable: false },
					{ name: 'expRaiseBegDate', index: 'expRaiseBegDate', resizable: true, sortable: false,formatter:dateFormatter},
					{ name: 'expRaiseEndDate', index: 'expRaiseEndDate', resizable: true, sortable: false,formatter:dateFormatter}
				  ],
		rowNum:20,        
		rowList:[20,30,50],  
		rownumbers : true,
		postData:{
			'sp[subPrdCode]': subPrdCode
		},
		rownumWidth : 50,
		prmNames : {
			search : "search",
			page : "pageNo",
			rows : "limit"
		},
		height : 'auto',
		width : 'auto',
		autowidth : true,
		shrinkToFit : true,
		viewrecords : true,
		cellEdit : false,
		shrinkToFit : true,
		grouping : false,
		jsonReader : {
			root : "items", // 结果集
			records : "total", // 总记录数
			total : "pageCount", // 总页数
			page : "pageNo", // 当前页
			repeatitems : false
		},
		multiselect : true,
		viewrecords : true,
		hiddengrid: false, //
		hidegrid: false, // 隐藏/显示按钮是否显示
		pager: "#prdSellChannelListPage",
		gridComplete: function() {
			
		}
	});
	var params = {'add' : 'addPrdSellChannelTemp','del' : 'delPrdSellChannelTemp'};
	var permissionButton = Permission_Button.configPermissionButton(params);
	console.log(permissionButton);
	var btnStr = Permission_Button.linkAllBtn(permissionButton);
	console.log(btnStr);
	var butArr = [];
	if(permissionButton.add){
		var caption = $(permissionButton.add).text();
		var icon = $(permissionButton.add).find("i").attr("class");
		var addPrams = {  
				id: "prdSellChannel_btnAdd",
				caption:caption,   
				buttonicon:icon,  
				position:"last", 
				onClickButton: function(){
					prdSellChannelAdd(subPrdCode);
				}
			}
		butArr.push(addPrams)
	}
	if(permissionButton.del){
		var caption = $(permissionButton.del).text();
		var icon = $(permissionButton.del).find("i").attr("class");
		var delPrams = {  
				id: "prdSellChannel_btnDel",
				caption:caption,   
				buttonicon:icon,  
				position:"last", 
				onClickButton: function(){
					batchDel(subPrdCode);
				}
			}
		butArr.push(delPrams)
	}
	var prdSellGrid  =  $("#prdSellChannelList").navGrid('#prdSellChannelListPage',{edit:false,add:false,del:false,search:false});
	if(butArr.length>0){
		for(var i=0;i<butArr.length;i++){
			prdSellGrid.navButtonAdd("#prdSellChannelListPage",butArr[i]);
		}
	}
}

function prdSellChannelAdd(subPrdCode){
	layer.open({
		  title: '新增销售渠道',
	      type: 2,
	      area: ['800px', '380px'], // 设置宽度，高度
	      shadeClose: true, //点击遮罩关闭
	      content: [encodeURI(SELL_PATH_PREFIX+"/prdSellChannelAdd.do?sp[subPrdCode]="+subPrdCode),'no'],//no代表不显示滚动条
	      isOutAnim: false,
	      resize: false,
	      end: function(){ 
	    	  queryPrdSellChannelByCondition(subPrdCode);
	    	 }
	    });
}

function batchDel(subPrdCode){
	var ids =  $("#prdSellChannelList").jqGrid('getGridParam', 'selarrrow');//选择多选
	if(ids.length == 0){
		ctools.alert('请选择需要删除的销售渠道信息!',"","warning");
		return;
	}
	ctools.confirm({text:"是否确认删除销售渠道信息？"},function(isConfirm){
		if(isConfirm){
			if (ids == null || ids == "") {
				return;
			}
			$.ajax({
				type: 'POST',
				url: SELL_PATH_PREFIX + "/delPrdSellChannel.do",
				dataType:'json',
				data:{
					"channelIds" : ids.join(",")
				},
				success: function(data){
						ctools.alert_sweet(data.message, "success", "" , function(){
							queryPrdSellChannelByCondition(subPrdCode);
						});
				},
				error:function(xhr){
					switch(xhr.status){
						case 403:sweetAlert("对不起，您无此权限！","","error");break;
						case 404:sweetAlert("对不起，无此页面！", "","error");break;
						case 500:sweetAlert("内部错误，请联系管理员！","","error");break;  
						case 504:sweetAlert("超时，请联系管理员！", "","error");break;  
						case 417:sweetAlert("内部错误，请联系管理员！", "","error");break;  
					}
				}
			});
		}
	});
}

//自动适应宽度
$(window).resize(function(){
	var parentWidth = $("#prdSellChannelListDiv").width();
	$("#prdSellChannelList").jqGrid('setGridWidth', parentWidth );
});

function queryPrdSellChannelByCondition(subPrdCode) {
	
	var postData = $("#prdSellChannelList").jqGrid("getGridParam", "postData");
    $.extend(postData,{
    	'sp[subPrdCode]': subPrdCode
    });
    $("#prdSellChannelList").jqGrid('setGridParam',{"datatype": "json"}).trigger("reloadGrid",[{page:1}]);//重新载入Grid表格
}

function dateFormatter(text, options, rData){
	if(text == null){
		return "";
	}
	return moment(text).format('YYYY-MM-DD');
}
